//
//  PushChannel.m
//  Movie
//
//  Created by cjy on 16/3/25.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "PushChannel.h"
#import "PushChannelTableViewCell.h"
#import "ChannelModel.h"
#import "MHNetWorkTask.h"
#import "MusicPlayerViewController.h"

@interface PushChannel ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *array;

@end

@implementation PushChannel

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    [self createTableView];
    [self handle];

    self.navigationItem.title = self.title2;
}

- (void)handle{
    self.array = [NSMutableArray array];
    NSMutableDictionary *dic =@{@"App-Id" : @"10101027",
                                @"Device-Id" : @"616b51f3e3729568db23640554991556",
                                @"Device-V" : @"aVBob25lIE9TXzkuMi4xXzY0MCoxMTM2XzEwMDAwMTAwMF9pUGhvbmUgNVM="}.mutableCopy;
    
    [MHNetWorkTask getWithURL:
     [NSString stringWithFormat:@"http://mapi.yinyuetai.com/channel/videos.json?D-A=0&channelId=%ld&detail=true&offset=0&order=VideoPubDate&size=10",
      (long)self.ida]withParameter:nil withHttpHeader:dic withResponseType:ResponseTypeJSON withSuccess:^(id result) {
     
         NSArray *arrvideos = [result objectForKey:@"videos"];
         for (NSDictionary *dic in arrvideos) {

             ChannelModel *model = [[ChannelModel alloc]init];
             model.posterPic = [dic objectForKey:@"posterPic"];
             
             model.title = [dic objectForKey:@"title"];
             model.artistsName = [dic objectForKey:@"artistName"];
             model.totalViews = [dic objectForKey:@"totalViews"];
             model.url = [dic objectForKey:@"url"];
             model.regdate = [dic objectForKey:@"regdate"];
             model.desc = [dic objectForKey:@"description"];
             model.ida = [dic objectForKey:@"id"];
             [self.array addObject:model];
             
         }
         
         [self.tableView reloadData];
         
     } withFail:^(NSError *error) {
         
         
     }];
}

- (void)createTableView{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    [self.tableView registerClass:[PushChannelTableViewCell class] forCellReuseIdentifier:@"pool"];
    
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.backgroundColor = [UIColor blackColor];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PushChannelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];

    if (indexPath.row == 0  || indexPath.row % 2== 0 ){
    
        cell.backgroundColor = [UIColor whiteColor];
    
    }else{
    
        cell.backgroundColor = [UIColor colorWithRed:241 /255.f green:240 /255.f blue:246 /255.f alpha:1];
    }
    

    cell.selectionStyle = UITableViewCellAccessoryNone;
    
    ChannelModel *model = self.array[indexPath.row];
    
    [cell passModel:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MusicPlayerViewController *player = [[MusicPlayerViewController alloc]init];
    
    ChannelModel *model = self.array[indexPath.row];
    
    player.url = model.url;
    player.arr = self.array[indexPath.row];
    player.desp = model.desc;
    
    player.posterPic = model.posterPic;
    player.ida = [model.ida integerValue];
    
    [self.navigationController pushViewController:player animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
